﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace App13._1
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }

        public void OnLoaded(object sender, EventArgs args)
        {
            TreeViewItem item = new TreeViewItem();
            AddNode(item, this);
            treeView.Items.Add(item);
        }

        public void AddNode(TreeViewItem item, DependencyObject node)
        {
            if (item == null || node == null)
                return;

            item.Header = node.GetType().ToString();

            int count = VisualTreeHelper.GetChildrenCount(node);
            for (int i = 0; i < count; i++)
            {
                DependencyObject obj = VisualTreeHelper.GetChild(node, i);
                TreeViewItem child = new TreeViewItem();
                AddNode(child, obj);
                item.Items.Add(child);
            }

            if (item.Items.Count != 0)
                item.IsExpanded = true;
        }
    }
}
